#pragma once
#include <iostream>
#include <string>
#include <ctemplate/template.h>
#include <vector>
// #include "oj_model.hpp"
#include "oj_model2.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";

    class View
    {
    public:
        View(){}
        ~View(){}

        void AllExpandHtml(const std::vector<struct Question>& questions,std::string* html)
        {
            //显示信息：题目编号、题目标题、题目难度
            //使用表格显示
        
            //先形成要渲染的网页路径
            std::string src_html = template_path + "all_questions.html";
            
            //再形成根数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for(const auto& q : questions)
            {
                //形成root的子字典
                ctemplate::TemplateDictionary* sub = root.AddSectionDictionary("question_list");
                //所有添加进子字典的数据经过循环遍历，会被添加到目标html文件"question_list"所对应的区域并依次展开
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
           }

           //获取被渲染的网页
           ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            //完成渲染功能
            tpl->Expand(html,&root);
        }

        void OneExpandHtml(const struct Question& q,std::string* html)
        {
            std::string src_html = template_path + "one_question.html";

            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);

            ctemplate::Template* tql = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            tql->Expand(html,&root);
        }
    };
}